package com.itao.spel;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * @author sjt
 * @Date 2024/12/12 11:27
 */
@Getter
@AllArgsConstructor
public enum RangEnum {

    GT(1, ">"),
    GE(2, ">="),
    EQ(3, "="),
    LT(4, "<"),
    LE(5, "<="),
    ;

    /**
     * 值
     */
    private final Integer value;

    /**
     * 符号
     */
    private final String symbol;

    public static String getSymbol(Integer value) {
        for (RangEnum rang : RangEnum.values()) {
            if (rang.getValue().equals(value)) {
                return rang.getSymbol();
            }
        }
        return null;
    }

    public static Boolean clac(
            Integer leftRange,
            Integer leftValue,
            Integer rightRange,
            Integer rightValue,
            Integer value
    ) {
        ExpressionParser parser = new SpelExpressionParser();
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("leftValue", leftValue);
        context.setVariable("rightValue", rightValue);
        context.setVariable("value", value);
        String condition = "&&";
        if ((leftRange == 4 || leftRange == 5) && (rightRange == 1 || rightRange == 2)) {
            condition = "||";
        }
        String expression = "#value" +
                getSymbol(leftRange) +
                "#leftValue"+
                condition +
                "#value" +
                getSymbol(rightRange)+
                "#rightValue";
        System.out.println(expression);
        return parser.parseExpression(expression).getValue(context, Boolean.class);
    }

    public static void main(String[] args) {
        //1大于  2大于等于 3等于  4小于 5小于等于
        System.out.println(clac(4, 3, 1, 5, 2));
    }
}